//
//  MLDNewFeatureViewController.swift
//  Sina
//
//  Created by 万世豪情 on 15/10/21.
//  Copyright © 2015年 万世豪情. All rights reserved.
//

import UIKit

class MLDNewFeatureViewController: UIViewController,UIScrollViewDelegate {

    var pageControl: UIPageControl?

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        //初始化
        let scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)
        scrollView.delegate = self
        //背景颜色
        scrollView.backgroundColor = UIColor.whiteColor()
        
        //开启分页
        scrollView.pagingEnabled = true
        
        //禁止水平滚动
        scrollView.showsHorizontalScrollIndicator = false

        let count = 4
        
        for i in 0..<count{
        
            //初始化image 
            let imageView = UIImageView()
            //设置图片
            imageView.image = UIImage(named: "new_feature_\(i + 1)")
            //设置大小
            imageView.size = scrollView.size
            //设置imageView的x
            imageView.x = CGFloat(i) * SCREENW
            //添加到scrollView
            scrollView.addSubview(imageView)
            
            //判断是不是最后一个View
            if i == count - 1 {
            
                //是最后一个
                setupLastPage(imageView)
            }
        }
        //设置内容大小
        scrollView.contentSize = CGSizeMake(CGFloat(count) * scrollView.width, 0)
//        把scrollView添加到View上
        view.addSubview(scrollView)
        
        //添加一个pageControl
        
        let control = UIPageControl()
        
        control.numberOfPages = count
        
        control.centerX = scrollView.width * 0.5
        
        control.y = SCREENH - 100
        
        control.pageIndicatorTintColor = UIColor.blackColor()
        
        control.currentPageIndicatorTintColor = UIColor.orangeColor()
        
        view.addSubview(control)
        
        self.pageControl = control

    }
    
    //setupLastPage
    private func setupLastPage(imageView: UIImageView){
    
        //开启用户交互
        imageView.userInteractionEnabled = true
        
        //进入微博按钮
        let enterButton = UIButton()
        
        //设置背景图
        enterButton.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        enterButton.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        //设置标题
        enterButton.setTitle("进入微博", forState: UIControlState.Normal)
        
        //设置frame
        enterButton.sizeToFit()
        enterButton.centerX = SCREENW * 0.5
        enterButton.y = SCREENH - 200
        
        imageView.addSubview(enterButton)
        
        //分享微博按钮
        let sharedButton = UIButton()
        //添加点击事件
        sharedButton.addTarget(self, action: "shareButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        //设置文字颜色
        sharedButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        //设置不同状态的图片
        sharedButton.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        sharedButton.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        
        //设置标题
        sharedButton.setTitle(" 分享到微博", forState: UIControlState.Normal)
        //设置大小
        sharedButton.sizeToFit()
        sharedButton.centerX = enterButton.centerX
        sharedButton.y = enterButton.y - 50
        
        
        imageView.addSubview(sharedButton)
        
    }
    
    @objc private func shareButtonClick(button: UIButton){
    
        button.selected = !button.selected
    }
    
    // MARK: - scorllViewDelegate
    
    func scrollViewDidScroll(scrollView: UIScrollView){
    
        //计算滑动到第几页
        let page = scrollView.contentOffset.x / scrollView.width
        
        self.pageControl?.currentPage = Int(page + 0.5)
    }

   
}












